package com.carcenter.personnel.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.carcenter.common.pojo.personnel.PermissionDto;
import com.carcenter.common.result.Result;
import com.carcenter.personnel.entity.Permissions;
import com.carcenter.common.pojo.personnel.PermissionsVo;
import com.carcenter.personnel.entity.RolePermissions;
import com.carcenter.personnel.service.IPermissionsService;
import com.carcenter.personnel.service.IRolePermissionsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author car
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/permissions")
public class PermissionsController {

    @Autowired
    IPermissionsService permissionsService;

    @Autowired
    IRolePermissionsService rolePermissionsService;

    @GetMapping("/getPermissionsByRoleId/{roleId}")
    public Result<List<PermissionDto>> getPermissionsByRoleId(@PathVariable Integer roleId) {
        LambdaQueryWrapper<RolePermissions> wrapper = Wrappers
                .lambdaQuery(RolePermissions.class)
                .eq(RolePermissions::getRoleId, roleId);
        List<RolePermissions> list = rolePermissionsService.list(wrapper);

        List<PermissionDto> permissionDtos = new ArrayList<>();
        List<Integer> permissionIds = new ArrayList<>();
        for (RolePermissions rolePermissions : list) {
            permissionIds.add(rolePermissions.getPermissionId());
        }

        List<Permissions> permissions = permissionsService.listByIds(permissionIds);
        for (Permissions permission : permissions) {
            PermissionDto permissionDto = new PermissionDto();
            permissionDto.setId(permission.getId());
            permissionDto.setPermissionName(permission.getPermissionName());
            permissionDtos.add(permissionDto);
        }
        return Result.ok(permissionDtos);
    }

    @GetMapping("/getAllpermissions")
    public Result<List<PermissionsVo>> getAllpermissions() {
        List<PermissionsVo> allpermissions = permissionsService.getAllpermissions();
        return Result.ok(allpermissions);
    }

    @GetMapping("/getPermissionCodesByRoleId/{roleId}")
    public Result<List<String>> getPermissionCodesByRoleId(@PathVariable Integer roleId) {
        LambdaQueryWrapper<RolePermissions> wrapper = Wrappers
                .lambdaQuery(RolePermissions.class)
                .eq(RolePermissions::getRoleId, roleId);
        List<RolePermissions> list = rolePermissionsService.list(wrapper);
        List<Integer> permissionIds = list.stream().map(RolePermissions::getPermissionId).collect(Collectors.toList());

        List<Permissions> permissions = permissionsService.listByIds(permissionIds);
        List<String> permissionCodes = new ArrayList<>();
        for (Permissions permission : permissions) {
            permissionCodes.add(permission.getCode());
        }
        return Result.ok(permissionCodes);


    }


}
